package com.dragon.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.handler.BusinessException;
import com.dragon.response.Result;
import com.dragon.response.ResultCode;
import com.dragon.system.entity.User;
import com.dragon.system.service.UserService;
import com.dragon.system.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/user")
@Api(value = "系统用户模块",tags = "系统用户接口")
//java解决跨越问题
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;
    @ApiOperation(value = "查询用户",notes = "查询所有用户")
    @GetMapping("/findUser")
    public Result findUser(){
        List<User> list = userService.list();
        return Result.ok().data("user",list);
    }

    @ApiOperation(value ="查询用户",notes = "通过id查询用户")
    @GetMapping("/{id}")
    public Result findUserByID(@PathVariable("id")Long id){
        User user = userService.getById(id);
        return Result.ok().data("user",user);
    }

    @ApiOperation(value = "查询用户",notes = "分页查询用户信息")
    @GetMapping("/findUserList")
    public Result findUserList(@RequestParam(required = true,defaultValue = "1")Integer current,
                               @RequestParam(required = true,defaultValue = "10")Integer size){
        Page<User> page = new Page<>(current, size);
        Page<User> userPage = userService.page(page);
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
    @ApiOperation(value = "查询用户",notes = "返回分页查询用户信息")
    @PostMapping("/findUserPage")
    public Result findUserPage(@RequestParam(required = true,defaultValue = "1")Integer current,
                               @RequestParam(required = true,defaultValue = "10")Integer size,
                               @RequestBody UserVo userVo){
        Page<User> page = new Page<>(current, size);
        QueryWrapper wrapper = getWrapper(userVo);
        IPage<User> userPage = userService.findUserPage(page, wrapper);
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    private QueryWrapper getWrapper(UserVo userVo) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();

        if (userVo !=null){
            if (!StringUtils.isEmpty(userVo.getUsername())){
                wrapper.eq("username",userVo.getUsername());
            }
            if (!StringUtils.isEmpty(userVo.getSex())){
                wrapper.eq("sex",userVo.getSex());
            }
            if (!StringUtils.isEmpty(userVo.getNickname())){
                wrapper.eq("nickname",userVo.getNickname());
            }
            if(!StringUtils.isEmpty(userVo.getEmail())){
                wrapper.eq("email",userVo.getEmail());
            }
            if (!StringUtils.isEmpty(userVo.getDepartmentId())){
                wrapper.eq("department_id",userVo.getDepartmentId());
            }
        }
        return wrapper;
    }

    /*@Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation(value = "用户列表" ,notes = "查询所有用户信息")
    public Result findUsers(){
        List<User> list = userService.list();
        return Result.ok().data("users",list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个用户",notes = "通过ID查询对应的用户信息")
    public Result getUserById(@PathVariable("id") Long id){
        User user = userService.getById(id);
        if(user!=null){
            return Result.ok().data("user",user);
        }else{
            throw new BusinessException(ResultCode.USER_NOT_FOUND_EXCEPTION.getCode(),ResultCode.USER_NOT_FOUND_EXCEPTION.getMessage());
        }
    }*/

    /*
    * 分页查询用户列表
    * */
     /*  @Resource
    private UserService userService;

    @GetMapping("/findUserList")
    public Result findUserList(@RequestParam(required = true,defaultValue = "1")Integer current,
                               @RequestParam(required = true,defaultValue = "10")Integer size){
        Page<User> page = new Page<>(current,size);
        Page<User> userPage = userService.page(page);
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
*/
    /* @Autowired
     private UserService userService;

     @GetMapping("/findUserList")
     public Result findUserList(@RequestParam(required = true,defaultValue = "1")Integer current,
                                @RequestParam(required = true,defaultValue = "10")Integer size){
         Page<User> page = new Page<>(current, size);
         Page<User> userPage = userService.page(page);
         long total = userPage.getTotal();
         List<User> records = userPage.getRecords();
         return Result.ok().data("total",total).data("records",records);
     }*/

   /* @PostMapping("/findUserPage")
    public Result findUserPage(@RequestParam(required = true,defaultValue = "1")Integer current,
                               @RequestParam(required = true,defaultValue = "10")Integer size,
                               @RequestBody UserVo userVo){
        Page<User> page = new Page<>(current,size);
        QueryWrapper<User> wrapper = getWrapper(userVo);
        IPage<User> userPage = userService.findUserPage(page,wrapper);
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    private QueryWrapper<User> getWrapper(UserVo userVo){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (userVo !=null){
            if (!StringUtils.isEmpty(userVo.getDepartmentId())){
                queryWrapper.eq("department_id",userVo.getDepartmentId());
            }
            if (!StringUtils.isEmpty(userVo.getEmail())){
                queryWrapper.eq("email",userVo.getEmail());
            }
            if (!StringUtils.isEmpty(userVo.getNickname())){
                queryWrapper.eq("nickname",userVo.getNickname());
            }
            if (!StringUtils.isEmpty(userVo.getUsername())){
                queryWrapper.eq("username",userVo.getUsername());
            }
            if (!StringUtils.isEmpty(userVo.getSex())){
                queryWrapper.eq("sex",userVo.getSex());
            }
        }
        return queryWrapper;
    }*/


}

